#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"


int main(void)
{

	uint32_t ui8PinData;
	uint32_t ui32ADC0Value[1];
	volatile uint32_t ui32TempAvg;
	volatile uint32_t ui32TempValueC;
	volatile uint32_t ui32TempValueF;
	ui8PinData = 6;


	SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ); // System clock of 400MHz/5 = 80MHz

	SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); ////// Enables the ADC0

	ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0); /////// Configures the triger source. Set for processor trigger.


	//// Enable 1 sampling, reading from temperature sensor
	ADCSequenceStepConfigure(ADC0_BASE,3,0,ADC_CTL_TS|ADC_CTL_IE|ADC_CTL_END);
	ADCSequenceEnable(ADC0_BASE, 3);


	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); ///// Enable PortF
	GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1); ////// Set pin1 (red LED) as out

	while(1)
	{
	   ADCIntClear(ADC0_BASE, 3);
	   ADCProcessorTrigger(ADC0_BASE, 3);

	   while(!ADCIntStatus(ADC0_BASE, 3, false)) ////// Stalls until it gets its ADC sample back
	   {
	   }

	   ADCSequenceDataGet(ADC0_BASE, 3, ui32ADC0Value); /// Gets the data acquired
	   ui32TempAvg = (ui32ADC0Value[0] + 2); /// No averaging
	   ui32TempValueC = (1475 - ((2475 * ui32TempAvg)) / 4096)/10;
	   ui32TempValueF = ((ui32TempValueC * 9) + 160) / 5;

	   if(ui32TempValueF>70)
	   GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, ui8PinData);
	   else  GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);

	}
}
